package com.meizu.asum.richtextsdk.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meizu.asum.richtextsdk.R;

import java.util.ArrayList;

/**
 * Description： 选择话题界面<br>
 * User：Xie JunWen <br>
 * Email：v-xiejunwen@meizu.com <br>
 * CreateTime：2018/3/6  10:09 <br>
 * Remark：无
 */
public class SelectSymbolActivity extends Activity {
    private TextView titleTextView;
    private LinearLayout contentLayout;
    private ArrayList<Button> symbolButtons;

    private String symbolType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectsymbol);

        symbolType = getIntent().getStringExtra("symbol_type");

        getWidgets();
        setWidgets();
        addListener();
    }

    private void getWidgets() {
        titleTextView = findViewById(R.id.activity_selectsymbol_title_textview);
        contentLayout = findViewById(R.id.activity_selectsymbol_content_layout);
        symbolButtons = new ArrayList<>();
    }

    private void setWidgets() {
        titleTextView.setText(symbolType);

        String[] symbolNames;
        if (symbolType.equals("#")) {
            symbolNames = new String[]{"魅族", "小米", "华为", "三星"};
        } else if (symbolType.equals("%")) {
            symbolNames = new String[]{"饿了么", "口碑", "美团"};
        } else if (symbolType.equals("$")) {
            symbolNames = new String[]{"哈罗", "摩拜", "OFO"};
        } else {
            symbolNames = new String[]{"美利达", "捷安特", "拉皮条"};
        }

        showSymbols(symbolNames);
    }

    private void showSymbols(String[] symbolNames) {
        for (int i = 0; i < symbolNames.length; i++) {
            Button symbolButton = new Button(this);
            contentLayout.addView(symbolButton);
            symbolButtons.add(symbolButton);

            symbolButton.setText(symbolNames[i]);
        }
    }

    private void addListener() {
        for (int i = 0; i < symbolButtons.size(); i++) {
            symbolButtons.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectComplete(((Button) v).getText().toString());
                }
            });
        }
    }

    private void selectComplete(String symbol) {
        Intent intent = new Intent();
        intent.putExtra("symbol_type", symbolType);
        intent.putExtra("symbol", symbol);
        this.setResult(10086, intent);

        finish();
    }
}
